<?php

namespace app\common\command;

use app\admin\service\auth\AdminService;
use app\common\cache\AdminAccountSafeCache;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use tmcore\services\SysClearService;
use Exception;

class System extends Command
{
    /**
     * 指令配置
     */
    protected function configure()
    {
        $this->setName('tm')
            ->setDescription('系统命令行')
            ->addArgument('op', Argument::OPTIONAL, "操作类型")
            ->addOption('account', null, Option::VALUE_REQUIRED, '账号')
            ->addOption('password', null, Option::VALUE_REQUIRED, '密码')
            ->addOption('reserve_day', null, Option::VALUE_REQUIRED, '保留天数');
    }

    /**
     * 执行指令
     * @param Input $input
     * @param Output $output
     * @return bool
     */
    protected function execute(Input $input, Output $output): bool
    {
        if (!$input->getArgument('op')) {
            echo "\n";
            echo "===============系统命令行==============\n";
            echo "(1) 修改后台密码   (2) 清除登录限制\n";
            echo "(3) 清除系统日志   (4) 清除系统缓存\n";
            echo "=======================================\n";
            echo "请携带命令编号如: php think tm 1\n";
            echo "\n";
        }

        try {
            switch (intval($input->getArgument('op'))) {
                case 1: // 修改后台密码
                    $this->changePassword($input);
                    break;
                case 2: // 清除登录限制
                    $this->cleanAccountSafe($input);
                    break;
                case 3: // 清除系统日志
                    $this->clearSysLog($input);
                    break;
                case 4: // 清除系统缓存
                    $this->cleanSysCache();
                    break;
                default:
                    throw new Exception('没找到“' . $input->getArgument('op') . '”相关命令!');
            }
            $output->writeln("Successful");
            return true;
        } catch ( Exception $e ) {
            $output->writeln("Failed: " . $e->getMessage());
            return false;
        }
    }

    /**
     * 修改后台密码
     * @param Input $input
     * @throws Exception
     */
    private function changePassword(Input $input): void
    {
        $account  = $input->getOption('account');
        $password = $input->getOption('password');

        if (!$account) throw new Exception('account参数缺失!');

        if (!$password) throw new Exception('password参数缺失!');

        if (strlen($account) < 1 || strlen($account) > 32) throw new Exception('账号必须在1~32个字符内!');

        $res = AdminService::changePassword($account, $password);
        if (!$res) throw new Exception(AdminService::getError('修改密码失败'));
    }

    /**
     * 清除登录限制
     * @param Input $input
     */
    private function cleanAccountSafe(Input $input): void
    {
        $account = $input->getOption('account');
        if (!$account) throw new Exception('account参数缺失!');

        (new AdminAccountSafeCache)->remove($account);
    }

    /**
     * 清除系统日志
     * @param Input $input
     */
    private function clearSysLog(Input $input): void
    {
        // 保留天数,默认保留30天
        $count = $input->getOption('reserve_day');
        $count = $count?:30;

        (new SysClearService)->deleteLog($count);
    }

    /**
     * 清除系统缓存
     */
    private function cleanSysCache(): void
    {
        (new SysClearService)->clearCache();
    }
}